package com.geeke.test.controller;

import java.util.List;

import org.camunda.bpm.engine.runtime.EventSubscription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.geeke.common.controller.BaseController;
import com.geeke.utils.ResultUtil;
import com.geeke.wf.service.WfUtils;

/**
 * 请假管理Controller
 * @author
 * @version
 */
@RestController
@RequestMapping(value = "/test/wfexample")
public class ExampleController extends BaseController {

    @Autowired
    private WfUtils<?> wfUtils;

    @GetMapping("/signal/{signalName}")
    public ResponseEntity<JSONObject> sendSignal(@PathVariable("signalName") String signalName) {
        wfUtils.sendSignal(signalName, null, null, null);
        return ResponseEntity.ok(ResultUtil.successJson());
    }

    @GetMapping("/event/{eventType}")
    public ResponseEntity<JSONObject> listEventSub(@PathVariable("eventType") String eventType) {
        eventType = "all".equals(eventType) ? null : eventType;
        List<EventSubscription> events = wfUtils.listEventSub(eventType);
        JSONArray arr = new JSONArray();
        events.forEach(item -> {
            JSONObject obj = new JSONObject();
            obj.put("created", item.getCreated());
            obj.put("activityId", item.getActivityId());
            obj.put("eventName", item.getEventName());
            obj.put("eventType", item.getEventType());
            obj.put("id", item.getId());
            obj.put("processInstanceId", item.getProcessInstanceId());
            obj.put("tenantId", item.getTenantId());
            arr.add(obj);
        });
        return ResponseEntity.ok(ResultUtil.successJson(arr));
    }

}
